﻿using Avalonia.Controls;
using Toolkit.Lib.CodeModels;
using Toolkit.Lib.Services;

namespace iOSClub.Toolkit.WindowApp.Controls;

public partial class AddMemberControl : UserControl
{
    public AddMemberControl()
    {
        InitializeComponent();
    }

    public MemberModel? Done()
    {
        var isError = false;
        if (string.IsNullOrEmpty(IdBox.Text))
        {
            IdErrorBlock.Text = "未填身份证号";
            isError = true;
        }

        if (string.IsNullOrEmpty(NameBox.Text))
        {
            NameErrorBlock.Text = "未填名字";
            isError = true;
        }

        if (isError)
            return default;

        if (!IDVerificationService.CheckName(NameBox.Text))
        {
            NameErrorBlock.Text = "姓名出错";
            isError = true;
        }

        if (!IDVerificationService.CheckId(IdBox.Text))
        {
            IdErrorBlock.Text = "身份证号出错";
            isError = true;
        }
        
        return isError ? default : new MemberModel() { Name = NameBox.Text, ID = IdBox.Text };
    }
}